public class Solution1763 {
    public String longestNiceSubstring(String s) {
        int[] f=new int[26];
        int[] g=new int[26];
        for (char c : s.toCharArray()) {
            if (c>='a' && c<='z'){
                f[c-'a']++;
            }else{
                g[c-'A']++;
            }
        }
        int c=-1;
        for (int i=0;i<26;i++){
            if (f[i]==0 && g[i]!=0 || f[i]!=0 && g[i]==0){
                c=i;
                break;
            }
        }
        if (c==-1){
            return s;
        }else{
            char t;
            if (f[c]>0){
                t=(char)(c+'a');
            }else{
                t=(char)(c+'A');
            }
            String[] strings=s.split(String.valueOf(t));
            String ans="";
            for (String string : strings) {
                String temp=longestNiceSubstring(string);
                if (temp.length()>ans.length()){
                    ans=temp;
                }
            }
            return ans;
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution1763().longestNiceSubstring("YazaAay"));
    }
}
